package cn.wolfcode.crm.serivce.impl;

import cn.wolfcode.crm.domain.ChangePoint;
import cn.wolfcode.crm.mapper.ChangePointMapper;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.serivce.IChangePointService;
import cn.wolfcode.crm.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;


@Service
public class ChangePointServiceImpl implements IChangePointService {
    @Autowired
    private ChangePointMapper mapper;

    @Override
    public void deleteByPrimaryKey(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public void saveOrUpdate(ChangePoint record) {
        if (record.getId() == null) {
            //保存操作
            mapper.insert(record);
        }else {
            mapper.updateByPrimaryKey(record);
        }
    }

    @Override
    public ChangePoint selectByPrimaryKey(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public PageResult query(QueryObject qo) {

//        Integer total = mapper.count(qo);
//        if (total > 0) {
//            List<ChangePoint> rows = mapper.query(qo);
//
//            return new PageResult(total,rows);
//        }
//        return new PageResult(0, Collections.EMPTY_LIST);
        return null;
    }

    @Override
    public List<ChangePoint> selectAll() {
        return mapper.selectAll();
    }

    @Override
    public void addPoint(ChangePoint changePoint) {
        ChangePoint old = mapper.selectByPrimaryKey(changePoint.getId());
        Integer number = old.getNumber();
        number=0;
        Integer newNumber = changePoint.getNumber();
        int i = number + newNumber;
        changePoint.setMark(changePoint.getMark());
        changePoint.setId(changePoint.getId());
       changePoint.setNumber(i);
       changePoint.setType("1");
       changePoint.setId(old.getId());
      /* changePoint.setMemberId();*/
       mapper.updateNewPoint(changePoint);
       mapper.addTotalPoint(changePoint);
    }

    @Override
    public void removePoint(ChangePoint changePoint) {
        ChangePoint old = mapper.selectByPrimaryKey(changePoint.getId());
        Integer number = old.getNumber();
        Integer newNumber = changePoint.getNumber();
        String mark = old.getMark();
        mark = "";
        int i = number - newNumber;
        if(i<0){
            mapper.addTotalPoint(changePoint);
        }else if(i>0){
            mapper.aremoveTotalPoint(changePoint);
        }
        Long d = changePoint.getId();
        changePoint.setId(d);
        changePoint.setMark(changePoint.getMark());
        changePoint.setNumber(i);
        changePoint.setType("0");
        changePoint.setId(old.getId());
        /* changePoint.setMemberId();*/
        mapper.updateNewPoint(changePoint);
    }
}
